import source.sharedmemory : SHMmanager;
import std.json;
import std.net.curl;
import std.conv;
import std.datetime;
import std.array;
import std.format;
import std.string;

int main(string[] args)
{
    string[] ex = split(args[2], " ");
    ex.length = 10;
    JSONValue settings = parseJSON(args[3]);

    //     0       1      2        3      4     5
    // username PRIVMSG #blah :something else else

    if (ex[3] == ":" ~ settings["prefix"].str ~ "la" ||
        ex[3] == ":" ~ settings["prefix"].str ~ "launch")
    {
        int next = 1;
        if (isNumeric(ex[4]))
        {
            next = to!int(ex[4]);
        }

        if (next <= 0 || next >= 100)
        {
            next = 1;
        }

        // http://launchlibrary.net/1.4/launch?next=1&mode=verbose

        string apiReturn = to!string(get("http://launchlibrary.net/1.4/launch?next=" ~ to!string(next) ~ "&mode=verbose"));
        JSONValue api = parseJSON(apiReturn);

        string green   = "\00309\002Go\002\003 - ";
        string red     = "\00304\002TBD\002\003 - ";
        string success = "\00311\002Success\002\003 - ";
        string failed  = "\00301\002Failure\002\003 - ";
        string hold    = "\00307\002Hold\002\003 - ";
        string flying  = "\00313\002Flying\002\003 - ";
        string partial = "\00314\002Partial Failure\002\003 - ";

        string launchName = "\002" ~ api["launches"][next - 1]["name"].str ~ "\002";
        string colour;
        switch (api["launches"][next -1]["status"].integer)
        {
            case 1: colour = green; break;
            case 2: colour = red; break;
            case 3: colour = success; break;
            case 4: colour = failed; break;
            case 5: colour = hold; break;
            case 6: colour = flying; break;
            case 7: colour = partial; break;
            default: colour = failed; break;
        }

        string windowString;
        {
            windowString ~= colour ~ launchName ~ " ";
            if (colour != failed && colour != partial && colour != flying && colour != failed && colour != success)
            {
                windowString ~= "- ";

                SysTime current = Clock.currTime();
                string isostart = api["launches"][next - 1]["isostart"].str;
                string isoend = api["launches"][next - 1]["isoend"].str;
                isostart.length--;
                isoend.length--;
                SysTime launchStart = DateTime.fromISOString(isostart);
                SysTime launchEnd = DateTime.fromISOString(isoend);

                Duration nowToLaunch = launchStart - current;
                Duration launchB = launchEnd - launchStart;
                auto toPass = nowToLaunch.split();
                auto elapsing = launchB.split();

                if (nowToLaunch.total!"minutes" > 0) {windowString ~= "in ";}
                if (toPass.weeks > 0) {windowString ~= to!string(toPass.weeks) ~ " weeks, ";}
                if (toPass.days > 0) {windowString ~= to!string(toPass.days) ~ " days, ";}
                if (toPass.hours > 0) {windowString ~= to!string(toPass.hours) ~ " hours, ";}
                if (toPass.minutes > 0) {windowString ~= to!string(toPass.minutes) ~ " minutes, ";}
                windowString.length -= 2;

                if (launchB.total!"minutes" > 0) {windowString ~= ", for ";}
                if (elapsing.days > 0) {windowString ~= to!string(toPass.days) ~ " days, ";}
                if (elapsing.hours > 0) {windowString ~= to!string(toPass.hours) ~ " hours, ";}
                if (elapsing.minutes >0) {windowString ~= to!string(toPass.minutes) ~ " minutes, ";}
                windowString.length -= 2;

                windowString ~= " - P:";
                if (api["launches"][next - 1]["probability"].integer == -1)
                {
                    windowString ~= "Unknown";
                }
                else
                {
                    windowString ~= to!string(api["launches"][next - 1]["probability"].integer);
                }
            }

            windowString ~= " | ";
            if (api["launches"][next - 1]["vidURLs"].array.length > 0)
            {
                windowString ~= api["launches"][next - 1]["vidURLs"][0].str;
            }
            else
            {
                windowString ~= "N/A";
            }
        }

        SHMmanager ioman = new SHMmanager(args[1], 0, 0);
        ioman.appendContents(JSONValue(
            ["method": "irc", "submethod": "send", "data": "PRIVMSG " ~ ex[2] ~ " :" ~ windowString]).toString);
    }
    return 0;
}
